package user

import (
	"fmt"
	"regexp"

	"Web-Shop/models"
	moUtils "Web-Shop/modules/utils"

	"github.com/astaxie/beego/utils"
)

func (c *UserController) ShowRegister() {
	c.TplName = "register.html"
}

func (c *UserController) Register() {
	c.TplName = "register.html"

	userName := c.GetString("user_name")
	pwd := c.GetString("pwd")
	cpwd := c.GetString("cpwd")
	email := c.GetString("email")

	if userName == "" || pwd == "" || cpwd == "" || email == "" {
		c.Data["errmsg"] = "数据不完整，请重新注册"
		return
	}

	reg, _ := regexp.Compile(`^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`)
	res := reg.FindString(email)
	if res == "" {
		c.Data["errmsg"] = "邮箱格式错误，请重新填写"
		return
	}
	if pwd != cpwd {
		c.Data["errmsg"] = "两次输入密码不一致，请重新输入"
		return
	}
	user := &models.User{
		Name:     userName,
		PassWord: pwd,
		Email:    email,
	}
	if err := user.Insert(); err != nil {
		fmt.Println(err)
		c.FlashNotice("用户名已存在")
		return
	}
	// 发送邮件
	emailRecord := new(models.UserEmailRecord)
	emailRecord.UseEmail = email
	if err := emailRecord.Insert(); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(emailRecord)
	fmt.Println(emailRecord.Salt)
	// 邮件配置
	emailConfig := `
	{"username":"1521274537@qq.com","password":"bibexnvsilmogabc","host":"smtp.qq.com","port":587}`
	emailConn := utils.NewEMail(emailConfig)
	emailConn.From = "1521274537@qq.com"
	emailConn.To = []string{email}
	emailConn.Subject = "thank you!" + userName
	emailConn.HTML = `<a href="http://192.168.199.104:8080/active?code=` + emailRecord.Salt + `">激活邮箱</a>`
	if err := emailConn.Send(); err != nil {
		fmt.Println(err)
		return
	}

	c.SetSession("userName", userName)
	c.Ctx.SetCookie("userName", userName, 50000, "/", "", true, true)
	c.Redirect("/", 302)
}

func (c *UserController) ActiveUser() {
	code := c.GetString("code")
	if len(code) != 20 {
		c.Redirect("/", 302)
	}
	emailRecord := &models.UserEmailRecord{
		Salt: code,
	}
	if err := emailRecord.Read(); err != nil {
		fmt.Println(err)
		return
	}
	if emailRecord.Used {
		return
	}
	if c.IsLogin {
		fmt.Println(c.User)
	} else {
		fmt.Println("weidenglu")
	}
	if emailRecord.UseEmail == c.User.Email {
		emailRecord.ActiveFunc()
		c.User.ActiveFunc()
	}
	c.Redirect("/", 302)
}

func (c *UserController) ShowLogin() {
	remember := c.Ctx.GetCookie("remember_username")
	if remember == "on" {
		c.Data["username"] = moUtils.DeBase64(c.Ctx.GetCookie("userName"))
	}
	c.TplName = "login.html"
}

func (c *UserController) Login() {
	c.TplName = "login.html"

	username := c.GetString("username")
	pwd := c.GetString("pwd")
	remember := c.GetString("remember")

	if username == "" || pwd == "" {
		c.FlashNotice("信息填写不完整")
		return
	}

	var user models.User
	reg, _ := regexp.Compile(`^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`)
	if reg.MatchString(username) {
		user.Email = username
		(&user).Read("Email")
	} else {
		user.Name = username
		(&user).Read("Name")
	}

	if user.PassWord == pwd {
		if remember == "on" {
			c.Ctx.SetCookie("remember_username", remember, 50000)
		} else {
			c.Ctx.SetCookie("remember_username", remember, -1)
		}
		c.SetSession("userName", user.Name)

		nameBase64 := moUtils.EnBase64(user.Name)
		c.Ctx.SetCookie("userName", nameBase64, 50000)
	} else {
		c.FlashNotice("信息填写有误")
		return
	}

	c.Redirect("/", 302)
}

func (c *UserController) Logout() {
	c.Ctx.Input.CruSession.Flush()
	c.Redirect("/", 302)
}
